package com.zryc.shop.pms.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.zryc.shop.pms.entity.SkuStock;
import com.zryc.shop.pms.service.ISkuStockService;
import com.zryc.shop.util.ResponseResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

import java.util.List;

/**
 * <p>
 * sku的库存 前端控制器
 * </p>
 *
 * @author LiYe
 * @since 2023-02-09
 */
@RestController
@RequestMapping("/pms/sku")
@Api(tags = "SKU库存管理接口列表")
public class SkuStockController {
    @Autowired
    private ISkuStockService skuStockService;

    @GetMapping("/{pid}")
    @ApiOperation("查询某个商品的SKU库存列表")
    public ResponseResult skuStockList(@PathVariable Long pid, @RequestParam(required = false) String keyword) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("product_id", pid);
        if (!StringUtils.isEmpty(keyword)) {
            queryWrapper.eq("sku_code", keyword);
        }
        return new ResponseResult(200, "查询完成", skuStockService.list(queryWrapper));
    }

    @PostMapping("/update/{pid}")
    @ApiOperation("更新SKU库存量")
    public ResponseResult updateSkuStock(@PathVariable Long pid, @RequestBody List<SkuStock> skuStockList) {
        for (SkuStock stock:skuStockList) {
            UpdateWrapper updateWrapper = new UpdateWrapper();
            updateWrapper.set("sku_code", stock.getSkuCode());
            updateWrapper.set("price", stock.getPrice());
            updateWrapper.set("stock", stock.getStock());
            updateWrapper.set("low_stock", stock.getLowStock());
            updateWrapper.eq("id", stock.getId());
            skuStockService.update(updateWrapper);
        }
        return new ResponseResult(200, "修改成功");
    }
}
